<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.example.pojo.User" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.example.DAO.DBUtilsDAO" %>
<%@ page import="com.example.DAO.impl.DBUtilsFactory" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>


<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>柒捌玖-后台</title>
  <style>

    body, html {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
    }

    .nav {
      width: 100%;
      height: 8em;
      background-color: black;
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center;
    }

    .nav p {
      margin: 0;
      padding: 0;
      font-size: 3em;
      color: aliceblue;
    }
    a{
      text-decoration: none;
      color: gray;
      margin-left: 2%;
      cursor: pointer;
    }

    a:hover{
      color: blue;
    }

    .container {
      display: flex;
    }

    .sidebar {
      width: 200px;
      border-right: 1px solid #ccc;
      padding: 10px;
    }

    .func-button {
      width: 100%;
      padding: 10px;
      margin: 5px 0;
      background-color: #f0f0f0;
      border: none;
      cursor: pointer;
      text-align: left;
    }

    .func-button:hover {
      background-color: #e0e0e0;
    }

    .func-button.active {
      background-color: #c0c0c0;
    }

    .content-area {
      flex: 1;
      padding: 20px;
    }

    .hidden-content {
      display: none;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    th, td {
      border: 1px solid #000;
      padding: 8px;
      text-align: center;

    }
    .dropdown {
      width: 200px;
      position: relative;
      display: inline-block;
    }

    .dropbtn {
      width: 100%;
      padding: 10px;
      margin: 5px 0;
      background-color: #f0f0f0;
      border: none;
      cursor: pointer;
      text-align: left;

    }

    .dropdown-content {
      display: none;
      position: absolute;
      background-color: #f9f9f9;
      min-width: 185px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
    }
  </style>
</head>
<body>
<header>
  <div class="nav">
    <p><b>柒捌玖———线上酒品店管理系统</b></p>
  </div>
</header>
<%
  User user = (User) session.getAttribute("user");
  if (user != null) {
%>
<div style="margin-left: 1%">
  欢迎, <%= user.getIdentity() %><%= user.getUsername() %>
  <a href="<%= request.getContextPath() %>/logout">退出</a>
</div>
<%
} else {
%>
<div>
  <a href="login.jsp">请登录/请注册</a>
</div>
<%
  }
%>
<div class="container">
  <div class="sidebar">
    <button class="func-button active"><a href="home.jsp">用户管理</a></button>
    <button class="func-button"><a href="Orders.jsp">订单管理</a></button>
    <div class="dropdown">
      <button class="dropbtn">商品管理</button>
      <div class="dropdown-content">
        <button class="func-button" onclick="showContent('ProductsManagement')">查看商品</button>
        <button class="func-button"><a href="AddProducts.jsp">添加商品</a></button>
      </div>
    </div>

  </div>
  <div class="content-area" id="content-area">
    <div id="UsersManagement" class="hidden-content">
      <center><h1>用户管理</h1></center>
      <table border="1">
        <tr>
          <th>头像</th>
          <th>用户名</th>
          <th>密码</th>
          <th>身份</th>
          <th>操作</th>
        </tr>
        <%
          DBUtilsDAO dbConnection = DBUtilsFactory.getDBConnection();
          String sql = "SELECT * FROM user";
          try (Connection conn = dbConnection.getConnection();
               PreparedStatement prepStmt = conn.prepareStatement(sql);
               ResultSet rs = prepStmt.executeQuery()) {

            while (rs.next()) {
              String username = rs.getString("user_name");
              String password = rs.getString("user_password");
              String identity = rs.getString("identity");
              String head_image = rs.getString("head_image");
        %>
        <%
          if(!identity.equals("管理员"))
          {
        %>
        <tr>

          <td><img src="<%= head_image %>" style="width: 50px;height: 50px"></td>

          <td><%= username %></td>
          <td><label>
            <input type="password" value="<%= password %>" style="border: none;font-size: 2em;width: 70px">
          </label></td>
          <td><%= identity %></td>

          <td><a href="deleteUser?username=<%= username %>">删除</a></td>

        </tr>
        <%
          }
        %>
        <%
            }
          } catch (SQLException e) {
            e.printStackTrace();
          }
        %>
      </table>
    </div>
    <div id="OrdersManagement" class="hidden-content">
      <center><h1>订单管理</h1></center>
      <table border="1">
        <tr>
          <th>订单编号</th>
          <th>商品图片</th>
          <th>商品名称</th>
          <th>用户名</th>
          <th>订单状态</th>
          <th>订单日期</th>
          <th>商品详情</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
        <%
          DBUtilsDAO dbConnection1 = DBUtilsFactory.getDBConnection();
          String sql1 = "SELECT * FROM orders";
          try (Connection conn = dbConnection1.getConnection();
               PreparedStatement prepStmt = conn.prepareStatement(sql1);
               ResultSet rs = prepStmt.executeQuery()) {

            while (rs.next()) {
              int orderId = rs.getInt("order_id");
              String productImage = rs.getString("product_image");
              String productName = rs.getString("product_name");
              String username = rs.getString("user_name");
              String status = rs.getString("status");
              String orderDate = rs.getString("order_date");
              String productDetails = rs.getString("product_details");
              double totalPrice = rs.getDouble("total_price");
        %>
        <tr>
          <td><%= orderId %></td>
          <td><img src="<%= productImage %>" style="width: 70px;height: 90px;"></td>
          <td><%= productName %></td>
          <td><%= username %></td>
          <td><%= status %></td>
          <td><%= orderDate %></td>
          <td><%= productDetails %></td>
          <td><%= totalPrice %></td>
          <td><a href="deleteOrder?orderId=<%= orderId %>" onclick="showOrderManagement()">删除</a></td>
        </tr>
        <%
            }
          } catch (SQLException e) {
            e.printStackTrace();
          }
        %>
      </table>
    </div>

    <div id="ProductsManagement" class="hidden-content">
      <div>
        <center><h1>商品管理</h1></center>
        <table border="1">
          <tr>
            <th>商品编号</th>
            <th>商品图片</th>
            <th>商品名称</th>
            <th>商品价格</th>
            <th>库存</th>
            <th>商品状态</th>
            <th>操作</th>
          </tr>
          <%
            DBUtilsDAO dbConnection2 = DBUtilsFactory.getDBConnection();
            String sql2 = "SELECT * FROM products,image where products.wine_name=image.wine_name";
            try (Connection conn = dbConnection2.getConnection();
                 PreparedStatement prepStmt = conn.prepareStatement(sql2);
                 ResultSet rs = prepStmt.executeQuery()) {

              while (rs.next()) {
                int productId = rs.getInt("product_id");
                String productImages = rs.getString("product_images");
                String wineName = rs.getString("wine_name");
                String status = rs.getString("status");
                int price = rs.getInt("price");
                int inventory = rs.getInt("product_inventory");

          %>
          <tr>
            <td><%= productId %></td>
            <td><img src=" <%= productImages %>" style="width: 70px;height: 90px;"></td>
            <td><%= wineName %></td>
            <td><%= price %></td>
            <td><%= inventory %></td>
            <td><%= status %></td>
            <td><a href="deleteProduct?product_id=<%= productId %>">删除</a></td>
          </tr>
          <%
              }
            } catch (SQLException e) {
              e.printStackTrace();
            }
          %>
        </table>
      </div>
    </div>
    <div id="AddProducts" class="hidden-content">
      <div>
        <form action="addProducts" method="post" style="text-align: center">
          <center><h1>添加商品</h1></center>
          商品编号：<input type="number" name="productId" placeholder="请输入商品编号"><br><br>
          商品名称：<input type="text" name="wineName" placeholder="请输入商品名称"><br><br>
          商品价格：<input type="number" name="price" placeholder="请输入商品价格"><br><br>
          库存：<input type="number" name="product_inventory" style="margin-left: 2.2%" placeholder="请输入库存数"><br><br>
          商品状态：<input type="text" name="status" placeholder="请输入当前商品状态" ><br><br><br>
          <input type="submit" value="添加商品" style="font-size: 17px; margin-left: 15px">
        </form>

      </div>
    </div>
  </div>
</div>

<script>
  function showContent(contentId) {
    const buttons = document.querySelectorAll('.func-button');
    buttons.forEach(button => {
      button.classList.remove('active');
    });

    const clickedButton = document.querySelector(`button[onclick="showContent('${contentId}')"]`);
    if (clickedButton) {
      clickedButton.classList.add('active');
    }

    const contentAreas = document.querySelectorAll('.hidden-content');
    contentAreas.forEach(contentArea => {
      contentArea.style.display = 'none';
    });

    const contentToShow = document.getElementById(contentId);
    if (contentToShow) {
      contentToShow.style.display = 'block';
    }
  }

  document.addEventListener('DOMContentLoaded', () => {
    showContent('ProductsManagement');
  });



  window.onload = function() {
    var urlParams = new URLSearchParams(window.location.search);
    if (urlParams.has('scrollTo')) {
      var elementId = urlParams.get('scrollTo');
      var element = document.getElementById(elementId);
      if (element) {
        element.scrollIntoView({ behavior: 'smooth' });
      }
    }
  };

  // scripts.js
  document.addEventListener('DOMContentLoaded', (event) => {
    const dropdownButton = document.querySelector('.dropbtn');
    const dropdownContent = document.querySelector('.dropdown-content');

    dropdownButton.addEventListener('click', () => {
      if (dropdownContent.style.display === 'block') {
        dropdownContent.style.display = 'none';
      } else {
        dropdownContent.style.display = 'block';
      }
    });


    window.addEventListener('click', (event) => {
      if (!event.target.closest('.dropdown')) {
        dropdownContent.style.display = 'none';
      }
    });


    dropdownContent.addEventListener('click', (event) => {
      event.stopPropagation();
    });

  });



</script>
</body>
</html>


